package com.lcq.gulimall.product.app;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

//import org.apache.shiro.authz.annotation.RequiresPermissions;
import com.lcq.gulimall.product.dao.AttrAttrgroupRelationDao;
import com.lcq.gulimall.product.entity.AttrAttrgroupRelationEntity;
import com.lcq.gulimall.product.vo.AttrGroupRelationVo;
import com.lcq.gulimall.product.vo.AttrGroupVo;
import com.lcq.gulimall.product.vo.AttrRespVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.lcq.gulimall.product.entity.AttrGroupEntity;
import com.lcq.gulimall.product.service.AttrGroupService;
import com.lcq.common.utils.PageUtils;
import com.lcq.common.utils.R;



/**
 * 属性分组
 *
 * @author lichuanqi
 * @email lichuanqi0086@163.com
 * @date 2021-02-25 19:28:13
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;

    @Autowired
    private AttrAttrgroupRelationDao attrAttrgroupRelationDao;
    /**
     * 列表
     */
    @RequestMapping("/list/{catId}")
    //@RequiresPermissions("product:attrgroup:list")
    public R list(@RequestParam Map<String, Object> params, @PathVariable("catId") Integer catId){
        //PageUtils page = attrGroupService.queryPage(params);
        PageUtils page = attrGroupService.queryPage(params,catId);
        return R.ok().put("page", page);
    }
    /**
     *
     * /product/attrgroup/{catelogId}/withattr
     * */
    @RequestMapping("/{catelogId}/withattr")
    //@RequiresPermissions("product:attrgroup:list")
    public R groupWithAttr(@PathVariable("catelogId") Long catelogId){
        //PageUtils page = attrGroupService.queryPage(params);
        List<AttrGroupVo> list = attrGroupService.getGroupWithAttr(catelogId);
        return R.ok().put("data", list);
    }


    /**
    * /api/product/attrgroup/1/attr/relation
    **/
    @RequestMapping("/{gourpId}/attr/relation")
    //@RequiresPermissions("product:attrgroup:list")
    public R attrRelationList(@PathVariable("gourpId") Long gourpId){
        //PageUtils page = attrGroupService.queryPage(params);
        List<AttrRespVo> List = attrGroupService.queryAttrRelationList(gourpId);
        return R.ok().put("data", List);
    }
    /**
     * /product/attrgroup/{attrgroupId}/noattr/relation
     * */
    @RequestMapping("/{attrgroupId}/noattr/relation")
    //@RequiresPermissions("product:attrgroup:list")
    public R noAttrRelationList(@RequestParam Map<String, Object> params, @PathVariable("attrgroupId") Long gourpId){
        //PageUtils page = attrGroupService.queryPage(params);
        PageUtils page = attrGroupService.queryNoAttrRelationPage(params,gourpId);
        return R.ok().put("data", page);
    }

    /**
     * /api/product/attrgroup/attr/relation
     * */
    @RequestMapping("/attr/relation")
    //@RequiresPermissions("product:attrgroup:list")
    public R attrRelation(@RequestBody AttrGroupRelationVo[] attrGroupRelationVos){
        attrGroupService.attrRelation(attrGroupRelationVos);
        return R.ok();
    }

    @PostMapping("/attr/relation/delete")
    //@RequiresPermissions("product:attrgroup:list")
    public R attrRelationDeleteBatch(@RequestBody AttrAttrgroupRelationEntity[] entities){
        //PageUtils page = attrGroupService.queryPage(params);
        attrAttrgroupRelationDao.deleteBatch(entities);
        return R.ok();
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
    //@RequiresPermissions("product:attrgroup:info")
    public R info(@PathVariable("attrGroupId") Long attrGroupId){
		AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);

        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("product:attrgroup:save")
    public R save(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("product:attrgroup:update")
    public R update(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("product:attrgroup:delete")
    public R delete(@RequestBody Long[] attrGroupIds){
		attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }

}
